package com.centling.smartSealForPhone.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;

import com.centling.smartSealForPhone.R;
import com.centling.smartSealForPhone.adapter.ChangeSealListAdapter;
import com.centling.smartSealForPhone.bean.ChangeSeal;
import com.centling.smartSealForPhone.bean.DeviceTypeBean;
import com.centling.smartSealForPhone.constants.GlobalData;
import com.centling.smartSealForPhone.constants.UrlConstants;
import com.centling.smartSealForPhone.utils.ControlUtil;
import com.centling.smartSealForPhone.utils.OkHttpUtil;
import com.centling.smartSealForPhone.utils.ShowToast;
import com.centling.smartSealForPhone.widget.AutoRecyclerView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;

public class SealChangeActivity extends TitleBarActivity {
    private List<ChangeSeal> list = new ArrayList<>();
    private int pageIndex;
    private PtrClassicFrameLayout ptrChangeList;
    private AutoRecyclerView rvChange;

    private DeviceTypeBean deviceTypeBean;

    @Override
    public void onResume() {
        super.onResume();
        rvChange.post(new Runnable() {
            @Override
            public void run() {
                ptrChangeList.autoRefresh();
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seal_change);
        setTitleBarText("换章");
//        setTitleBarRight(R.drawable.iv_add);
        setTitleBarRightPadding(R.drawable.iv_add_plus);
        setTitleBarRightClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, SealChangeApplyActivity.class);
                startActivity(intent);
            }
        });

        initView();
    }

    public void initView() {
        ptrChangeList = (PtrClassicFrameLayout) findViewById(R.id.ptr_seal_change_list);
        rvChange = (AutoRecyclerView) findViewById(R.id.rv_seal_change_list);
        rvChange.setLayoutManager(new GridLayoutManager(this, 1));
        ChangeSealListAdapter adapter = new ChangeSealListAdapter(mContext, list);
        rvChange.setAdapter(adapter);

        ptrChangeList.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout ptrFrameLayout) {
                sendRequestToGetStampList(true);
            }
        });
        rvChange.setLoadDataListener(new AutoRecyclerView.LoadDataListener() {
            @Override
            public void onLoadMore() {
                sendRequestToGetStampList(false);
            }
        });
        adapter.setOnStealChangeListener(new ChangeSealListAdapter.OnStealChangeListener() {
            @Override
            public void onClick(ChangeSeal item) {
                sendRequestToCheckIsChange(item);
            }
        });
        adapter.setOnDetailListener(new ChangeSealListAdapter.OnDetailListener() {
            @Override
            public void onDetailClick(View view, int position) {
                Intent intent = new Intent(mContext, SealChangeDetailActivity.class);
                String jsonString = new Gson().toJson(list.get(position));
                intent.putExtra("changeSeal", jsonString);
                startActivity(intent);
            }
        });
    }

    private void sendRequestToGetStampList(final boolean isRefresh) {
        if (isRefresh) {
            pageIndex = 0;
        }
        getEnqueue(UrlConstants.GET_STEAL_LIST + pageIndex, new OkHttpUtil.NetCallBack() {
            @Override
            public void onSucceed(String json) {
                JSONObject data;
                try {
                    data = new JSONObject(json);
                    if (isRefresh) {
                        list.clear();
                        rvChange.setLoadDataListener(new AutoRecyclerView.LoadDataListener() {
                            @Override
                            public void onLoadMore() {
                                sendRequestToGetStampList(false);
                            }
                        });
                    }
                    List<ChangeSeal> tempList;
                    tempList = new Gson().fromJson(data.getJSONArray("rows").toString(), new TypeToken<List<ChangeSeal>>() {
                    }.getType());
                    list.addAll(tempList);
                    rvChange.getAdapter().notifyDataSetChanged();
                    ptrChangeList.refreshComplete();
                    if (list.size() < 20) {
                        rvChange.loadMoreComplete(true);
                    } else {
                        rvChange.loadMoreComplete(false);
                    }
                    pageIndex++;
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailed(String reason) {
                ShowToast.shortTime(reason);
                ptrChangeList.refreshComplete();
                rvChange.loadMoreComplete(false);
            }
        });
    }

    private void sendRequestToCheckIsChange(final ChangeSeal item) {
        getEnqueue(UrlConstants.GET_DEVICE_STATE + GlobalData.REPLACESEAL,
                new OkHttpUtil.NetCallBack() {
                    @Override
                    public void onSucceed(String json) {
                        deviceTypeBean = new Gson().fromJson(json, DeviceTypeBean.class);
                        if (deviceTypeBean.isAllowed()) {
                            Intent intent = new Intent(mContext, SealChangeTipsActivity.class);
                            intent.putExtra("applyId", item.getReplaceSealApplyId());
                            startActivity(intent);
                            finish();
                        } else {
                            ShowToast.shortTime(deviceTypeBean.getMessage());
                        }
                    }


                    @Override
                    public void onFailed(String reason) {
                        ShowToast.shortTime(reason);
                    }
                });
    }
}
